home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1998…eptember: Technology Seed / September 98 ADC Seed CD.toast / Language Analysis Manager / DarumaDR1Package / Examples / LanguageAnalysisTestApp / Sources / ConsoleWindow.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-03-27  |  27.7 KB  |  1,013 lines  |  [TEXT/CWIE]

  1. /*
  2.     File:        ConsoleWindow.c
  3.  
  4.      Contains:    Sample code for Language Analysis Manager.
  5.  
  6.      Version:    Technology:    System 8
  7.                  Release:    Daruma Developer Release 1
  8.  
  9.      Copyright:    1998 by Apple Computer, Inc., all rights reserved
  10.  
  11.      Contact:    daruma@apple.com
  12.  
  13. */
  14.  
  15.  
  16. #include "ConsoleWindow.h"
  17.  
  18. #include <Fonts.h>
  19. #include <Controls.h>
  20. #include <TextUtils.h>
  21. #include <TextEdit.h>
  22. #include <Resources.h>
  23.  
  24. // ========================================================================================
  25. // Private definitions
  26. // ========================================================================================
  27. enum
  28. {
  29.     kCWTextMargin            = 4,
  30.     kCWMaxTEWidth            = 1280,
  31.     kCMGrowMax                = 0x7FFF,
  32.     kCMHorizScrollUnit        = 8,
  33.     kCWSignature            = 'cnsl'
  34. };
  35.  
  36. // AdjustConsoleWindow()'s operands
  37. enum
  38. {
  39.     kCWCreateWindow,
  40.     kCWResizeWindow,
  41.     kCWChangeAttribute,
  42.     kCWInputText,
  43.     kCWClearText
  44. };
  45.  
  46. struct OpaqueConsoleWindowRec
  47. {
  48.     WindowRecord    windowRec;
  49.     OSType            signature;
  50.     TEHandle        textHandle;
  51.     ControlRef        vScroll;
  52.     ControlRef        hScroll;
  53. };
  54. typedef struct OpaqueConsoleWindowRec OpaqueConsoleWindowRec, *ConsoleWindowPtr;
  55.  
  56.  
  57. // ========================================================================================
  58. // Prototypes for private functions
  59. // ========================================================================================
  60.  
  61. static OSStatus    CreateConsoleWindowCommon( WindowRef window, StringPtr fontName, SInt16 fontSize, ConsoleWindowPtr console );
  62. static void        GetTERect( WindowRef window, Rect *destRect, Rect *viewRect );
  63. static void        GetScrollBarRect ( WindowRef window, Rect *vScrollRect, Rect *hScrollRect );
  64. static void        AdjustViewRect ( TEHandle teH );
  65. static void        AdjustConsoleWindow( ConsoleWindowPtr console, SInt16 operand );
  66. static void        UpdateTEAttribute( ConsoleWindowPtr console, Boolean redraw );
  67. static SInt32    ConsoleWindowCStrLen( const char *str );
  68. static Boolean    IsConsoleWindow( WindowRef window );
  69. static void        DoConsoleWindowMouseDown( ConsoleWindowPtr console, EventRecord *theEvent, SInt16 partCode, Boolean *closed );
  70. static void        DoConsoleWindowUpdate( ConsoleWindowPtr console );
  71. static void        DoConsoleWindowActivate( ConsoleWindowPtr console, Boolean toActive );
  72. static void        DoConsoleWindowGrow( ConsoleWindowPtr console, Point startPt );
  73. static void        ResizeConsoleWindow( ConsoleWindowPtr console );
  74. static void        DoConsoleWindowContent( ConsoleWindowPtr console, Point startPt );
  75. static pascal void MyVScrollFilter( ControlRef control, SInt16 part );
  76. static pascal void MyHScrollFilter( ControlRef control, SInt16 part );
  77.  
  78.  
  79. /******************************************************************************************/
  80. #pragma mark --------------------------
  81. #pragma mark • Public Functions
  82. #pragma mark --------------------------
  83.  
  84. // ========================================================================================
  85. // GetNewConsoleWindow
  86. // ========================================================================================
  87. OSStatus
  88. GetNewConsoleWindow( SInt16 resID, WindowRef behind, StringPtr fontName,
  89.                      SInt16 fontSize, ConsoleWindowPtr *console )
  90. {
  91.     OSStatus            err;
  92.     WindowRef            window = NULL;
  93.     ConsoleWindowPtr    newConsole = NULL;
  94.     
  95.     newConsole = (ConsoleWindowPtr)NewPtrClear( sizeof( OpaqueConsoleWindowRec));
  96.     if ( newConsole == NULL) { err = MemError(); goto errExit; }
  97.     
  98.     window = GetNewWindow( resID, &newConsole->windowRec, behind);
  99.     if ( window == NULL) { err = ResError(); goto errExit; }
  100.     
  101.     err = CreateConsoleWindowCommon( window, fontName, fontSize, newConsole);
  102.     if ( err != noErr) goto errExit;
  103.     
  104.     *console = newConsole;
  105.     return noErr;
  106.  
  107. errExit:
  108.     if ( window != NULL) CloseWindow( window);
  109.     if ( newConsole != NULL) DisposePtr( (Ptr)newConsole);
  110.     *console = NULL;
  111.     return err;
  112. }
  113.  
  114. // ========================================================================================
  115. // NewConsoleWindow
  116. // ========================================================================================
  117. OSStatus
  118. NewConsoleWindow( const Rect *windRect, StringPtr title, Boolean visible,
  119.                   SInt16 wDefProcID, WindowRef behind, Boolean goAwayFlag, long refcon,
  120.                   StringPtr fontName, SInt16 fontSize, ConsoleWindowPtr *console )
  121. {
  122.     OSStatus            err;
  123.     WindowRef            window = NULL;
  124.     ConsoleWindowPtr    newConsole = NULL;
  125.     
  126.     newConsole = (ConsoleWindowPtr)NewPtrClear( sizeof( OpaqueConsoleWindowRec));
  127.     if ( newConsole == NULL) { err = MemError(); goto errExit; }
  128.     
  129.     window = NewWindow( &newConsole->windowRec, windRect, title, visible, wDefProcID,
  130.                         behind, goAwayFlag, refcon);
  131.     if ( window == NULL) { err = MemError(); goto errExit; }
  132.     
  133.     err = CreateConsoleWindowCommon( window, fontName, fontSize, newConsole);
  134.     if ( err != noErr) goto errExit;
  135.     
  136.     *console = newConsole;
  137.     return noErr;
  138.  
  139. errExit:
  140.     if ( window != NULL) CloseWindow( window);
  141.     if ( newConsole != NULL) DisposePtr( (Ptr)newConsole);
  142.     *console = NULL;
  143.     return err;
  144. }
  145.  
  146.  
  147. // ========================================================================================
  148. // DisposeConsoleWindow
  149. // ========================================================================================
  150. void
  151. DisposeConsoleWindow( ConsoleWindowPtr console )
  152. {
  153.     if ( console != NULL)
  154.     {
  155.         CloseWindow( GetWindowFromConsoleWindow( console));
  156.         TEDispose( console->textHandle);
  157.         DisposePtr( (Ptr)console);
  158.     }
  159. }
  160.  
  161.  
  162. // ========================================================================================
  163. // ProcessConsoleWindowEvent
  164. // ========================================================================================
  165. Boolean
  166. ProcessConsoleWindowEvent( EventRecord *theEvent, Boolean *closed )
  167. {
  168.     Boolean                processEvent = false;
  169.     WindowRef            whichWindow;
  170.     SInt16                partCode;
  171.     ConsoleWindowPtr    console;
  172.     
  173.     *closed = false;
  174.     
  175.     switch ( theEvent->what )
  176.     {
  177.     case mouseDown:
  178.         partCode = FindWindow( theEvent->where, &whichWindow);
  179.         console = GetConsoleWindowFromWindow( whichWindow);
  180.         if ( console != NULL)
  181.         {
  182.             DoConsoleWindowMouseDown( console, theEvent, partCode, closed);
  183.             processEvent = true;
  184.         }
  185.         break;
  186.         
  187.     case updateEvt:
  188.         whichWindow = (WindowRef)theEvent->message;
  189.         console = GetConsoleWindowFromWindow( whichWindow);
  190.         if ( console != NULL)
  191.         {
  192.             DoConsoleWindowUpdate( console);
  193.             processEvent = true;
  194.         }
  195.         break;
  196.         
  197.     case activateEvt:
  198.         whichWindow = (WindowRef)theEvent->message;
  199.         console = GetConsoleWindowFromWindow( whichWindow);
  200.         if ( console != NULL)
  201.         {
  202.             DoConsoleWindowActivate( console, ( theEvent->modifiers & activeFlag) != 0);
  203.             processEvent = true;
  204.         }
  205.         break;
  206.         
  207.     case osEvt:
  208.         if ( ( (theEvent->message & osEvtMessageMask) >> 24) == suspendResumeMessage)
  209.         {
  210.             console = GetConsoleWindowFromWindow( FrontWindow());
  211.             if ( console != NULL)
  212.             {
  213.                 DoConsoleWindowActivate( console, (theEvent->message & resumeFlag) != 0);
  214.             }
  215.         }
  216.         
  217.     default:
  218.         break;
  219.     }
  220.  
  221.     return processEvent;
  222. }
  223.  
  224.  
  225. // ========================================================================================
  226. // SetConcoleWindowFont
  227. // ========================================================================================
  228. void
  229. SetConcoleWindowFont( ConsoleWindowPtr console, StringPtr fontName, Boolean redraw )
  230. {
  231.     GrafPtr        savePort;
  232.     SInt16        fontNum;
  233.     FontInfo    fontInfo;
  234.     
  235.     GetPort( &savePort);
  236.     
  237.     GetFNum( fontName, &fontNum);
  238.     
  239.     SetPort( (WindowRef)console);
  240.     TextFont( fontNum);
  241.     
  242.     GetFontInfo( &fontInfo);
  243.     (*console->textHandle)->txFont = fontNum;
  244.     (*console->textHandle)->fontAscent = fontInfo.ascent;
  245.     (*console->textHandle)->lineHeight = fontInfo.ascent + fontInfo.descent + fontInfo.leading;
  246.     
  247.     UpdateTEAttribute( console, redraw);
  248.     
  249.     SetPort( savePort);
  250. }
  251.  
  252.  
  253. // ========================================================================================
  254. // SetConcoleWindowFontSize
  255. // ========================================================================================
  256. void
  257. SetConcoleWindowFontSize( ConsoleWindowPtr console, SInt16 fontSize, Boolean redraw )
  258. {
  259.     GrafPtr        savePort;
  260.     FontInfo    fontInfo;
  261.     
  262.     GetPort( &savePort);
  263.     
  264.     SetPort( (WindowRef)console);
  265.     TextSize( fontSize);
  266.     
  267.     GetFontInfo( &fontInfo);
  268.     (*console->textHandle)->txSize = fontSize;
  269.     (*console->textHandle)->fontAscent = fontInfo.ascent;
  270.     (*console->textHandle)->lineHeight = fontInfo.ascent + fontInfo.descent + fontInfo.leading;
  271.     
  272.     UpdateTEAttribute( console, redraw);
  273.     
  274.     SetPort( savePort);
  275. }
  276.  
  277.  
  278. // ========================================================================================
  279. // PutPStringConsoleWindow
  280. // ========================================================================================
  281. void
  282. PutPStringConsoleWindow ( ConsoleWindowPtr console, StringPtr theString )
  283. {
  284.     SInt32        maxLength;
  285.     
  286.     maxLength = (*console->textHandle)->teLength;
  287.     
  288.     TESetSelect( maxLength, maxLength, console->textHandle);
  289.     TEInsert( &theString[1], theString[0], console->textHandle);
  290.     
  291.     AdjustConsoleWindow( console, kCWInputText);
  292. }
  293.  
  294.  
  295. // ========================================================================================
  296. // PutCStringConsoleWindow
  297. // ========================================================================================
  298. void
  299. PutCStringConsoleWindow ( ConsoleWindowPtr console, const char *theString )
  300. {
  301.     SInt32        maxLength;
  302.     
  303.     maxLength = (*console->textHandle)->teLength;
  304.     
  305.     TESetSelect( maxLength, maxLength, console->textHandle);
  306.     TEInsert( theString, ConsoleWindowCStrLen( theString), console->textHandle);
  307.     
  308.     AdjustConsoleWindow( console, kCWInputText);
  309. }
  310.  
  311.  
  312. // ========================================================================================
  313. // ClearConsoleWindowContent
  314. // ========================================================================================
  315. void
  316. ClearConsoleWindowContent( ConsoleWindowPtr console )
  317. {
  318.     SInt32        maxLength;
  319.     
  320.     if ( console != NULL)
  321.     {
  322.         maxLength = (*console->textHandle)->teLength;
  323.         
  324.         if ( maxLength != 0)
  325.         {
  326.             TESetSelect( 0, maxLength, console->textHandle);    // select all
  327.             TEDelete( console->textHandle);
  328.             AdjustConsoleWindow( console, kCWClearText);
  329.         }
  330.     }
  331. }
  332.  
  333.  
  334. // ========================================================================================
  335. // GetConsoleWindowFromWindow
  336. // ========================================================================================
  337. ConsoleWindowPtr
  338. GetConsoleWindowFromWindow( WindowRef window )
  339. {
  340.     if ( IsConsoleWindow( window))
  341.     {
  342.         return (ConsoleWindowPtr)window;
  343.     }
  344.     else
  345.     {
  346.         return NULL;
  347.     }
  348. }
  349.  
  350.  
  351. // ========================================================================================
  352. // GetWindowFromConsoleWindow
  353. // ========================================================================================
  354. WindowRef
  355. GetWindowFromConsoleWindow( ConsoleWindowRef console )
  356. {
  357.     return (WindowRef)console;
  358. }
  359.  
  360.  
  361. /******************************************************************************************/
  362. #pragma mark --------------------------
  363. #pragma mark • Private Functions
  364. #pragma mark --------------------------
  365.  
  366. // ========================================================================================
  367. // CreateConsoleWindowCommon
  368. // ========================================================================================
  369. static OSStatus
  370. CreateConsoleWindowCommon( WindowRef window, StringPtr fontName,
  371.                            SInt16 fontSize, ConsoleWindowPtr console )
  372. {
  373.     OSStatus        err;
  374.     GrafPtr            savePort;
  375.     Rect            viewRect, destRect;
  376.     Rect            vScrollRect, hScrollRect;
  377.     SInt16            fontNum;
  378.     
  379.     GetPort( &savePort);
  380.     SetPort( window);
  381.     
  382.     GetFNum( fontName, &fontNum);
  383.     TextFont( fontNum);
  384.     TextSize( fontSize);
  385.     
  386.     //------------------------------------------------------------------------------
  387.     // Initialize values
  388.     //
  389.     console->textHandle    = NULL;
  390.     console->vScroll    = NULL;
  391.     console->hScroll    = NULL;
  392.  
  393.     //------------------------------------------------------------------------------
  394.     // Create scroll bars
  395.     //
  396.     GetScrollBarRect( window, &vScrollRect, &hScrollRect);
  397.     
  398.     console->vScroll = NewControl( window, &vScrollRect, "\p", true, 0, 0, 0, scrollBarProc, 0L);
  399.     if ( console->vScroll == NULL) { err = MemError(); goto errExit; }
  400.     
  401.     console->hScroll = NewControl( window, &hScrollRect, "\p", true, 0, 0, 0, scrollBarProc, 0L);
  402.     if ( console->hScroll == NULL) { err = MemError(); goto errExit; }
  403.  
  404.     //------------------------------------------------------------------------------
  405.     // Create and set TextEdit handle
  406.     //
  407.     GetTERect( window, &destRect, &viewRect);
  408.     console->textHandle = TENew( &destRect, &viewRect);
  409.     if ( console->textHandle == NULL) { err = MemError(); goto errExit; }
  410.     
  411. //    TEAutoView( true, console->textHandle);    <3>
  412.     TEDeactivate( console->textHandle);
  413.     
  414.     //------------------------------------------------------------------------------
  415.     // Set signature
  416.     //
  417.     console->signature = kCWSignature;
  418.     
  419.     //------------------------------------------------------------------------------
  420.     // Adjust each gadjet
  421.     //
  422.     AdjustConsoleWindow( console, kCWCreateWindow);
  423.  
  424.     SetPort( savePort);
  425.     
  426.     ShowWindow( window);
  427.     
  428.     return noErr;
  429.  
  430. errExit:
  431.     if ( console->textHandle != NULL) TEDispose( console->textHandle);
  432.     if ( console->vScroll != NULL) DisposeControl( console->vScroll);
  433.     if ( console->hScroll != NULL) DisposeControl( console->hScroll);
  434.     SetPort( savePort);
  435.     return err;    
  436. }
  437.  
  438.  
  439. // ========================================================================================
  440. // GetTERect
  441. // ========================================================================================
  442. static void
  443. GetTERect( WindowRef window, Rect *destRect, Rect *viewRect )
  444. {
  445.     Rect    windowRect = window->portRect;
  446.     
  447.     InsetRect( &windowRect, kCWTextMargin, kCWTextMargin);
  448.     windowRect.bottom -= 15;
  449.     windowRect.right -= 15;
  450.     
  451.     if ( viewRect != NULL)
  452.     {
  453.         *viewRect = windowRect;
  454.     }
  455.     
  456.     if ( destRect != NULL)
  457.     {
  458.         *destRect = windowRect;
  459.         destRect->right = destRect->left + kCWMaxTEWidth;
  460.     }
  461. }
  462.  
  463.  
  464. // ========================================================================================
  465. // GetScrollBarRect
  466. // ========================================================================================
  467. static void
  468. GetScrollBarRect( WindowRef window, Rect *vScrollRect, Rect *hScrollRect )
  469. {
  470.     Rect    windowRect = window->portRect;
  471.     
  472.     if ( vScrollRect != NULL)
  473.     {
  474.         SetRect( vScrollRect, windowRect.right - 15, windowRect.top - 1,
  475.                               windowRect.right + 1, windowRect.bottom - 14);
  476.     }
  477.  
  478.     if ( hScrollRect != NULL)
  479.     {
  480.         SetRect( hScrollRect, windowRect.left - 1, windowRect.bottom - 15,
  481.                               windowRect.right - 14, windowRect.bottom + 1);
  482.     }
  483. }
  484.  
  485.  
  486. // ========================================================================================
  487. // AdjustViewRect
  488. //
  489. // Update the TERec's view rect so that it is the greatest multiple of
  490. // the lineHeight that still fits in the old viewRect.
  491. // ========================================================================================
  492. static void
  493. AdjustViewRect( TEHandle teH )
  494. {
  495.     (*teH)->viewRect.bottom = ( ( ( (*teH)->viewRect.bottom - (*teH)->viewRect.top)
  496.                                   / (*teH)->lineHeight) * (*teH)->lineHeight)
  497.                               + (*teH)->viewRect.top;
  498. }
  499.  
  500.  
  501. // ========================================================================================
  502. // AdjustConsoleWindow
  503. // ========================================================================================
  504. static void
  505. AdjustConsoleWindow( ConsoleWindowPtr console, SInt16 operand )
  506. {
  507.     SInt16        oldValue, oldMaxValue;
  508.     SInt16        newValue, newMaxValue, lines;
  509.     SInt16        deltaV, deltaH;
  510.     TEHandle    teH = console->textHandle;
  511.     
  512.     //------------------------------------------------------------------------------
  513.     // Hide both controls
  514.     //
  515. //    HideControl( console->vScroll);
  516. //    HideControl( console->hScroll);
  517.     
  518.     if ( operand == kCWCreateWindow || operand == kCWResizeWindow)
  519.     {
  520.         Rect        viewRect;
  521.         Rect        vScrollRect, hScrollRect;
  522.         
  523.         GetTERect( (WindowRef)console, NULL, &viewRect);
  524.         (*teH)->viewRect = viewRect;
  525.         AdjustViewRect( teH);
  526.         
  527.         //------------------------------------------------------------------------------
  528.         // Resize both scroll bars
  529.         //
  530.         GetScrollBarRect( (WindowRef)console, &vScrollRect, &hScrollRect);
  531.         
  532.         MoveControl( console->vScroll, vScrollRect.left, vScrollRect.top);
  533.         SizeControl( console->vScroll, vScrollRect.right - vScrollRect.left,
  534.                                        vScrollRect.bottom - vScrollRect.top);
  535.  
  536.         MoveControl( console->hScroll, hScrollRect.left, hScrollRect.top);
  537.         SizeControl( console->hScroll, hScrollRect.right - hScrollRect.left,
  538.                                        hScrollRect.bottom - hScrollRect.top);
  539.  
  540.         //------------------------------------------------------------------------------
  541.         // Adjust horizontal scroll bars
  542.         //
  543.         oldValue = GetControlValue( console->hScroll);
  544.         oldMaxValue = GetControlMaximum( console->hScroll);
  545.         
  546.         newMaxValue = kCWMaxTEWidth - ( (*teH)->viewRect.right - (*teH)->viewRect.left);
  547.         if ( newMaxValue < 0 ) newMaxValue = 0;
  548.         SetControlMaximum( console->hScroll, newMaxValue);
  549.         
  550.         newValue = (*teH)->viewRect.left - (*teH)->destRect.left;
  551.         if ( newValue < 0 )
  552.             newValue = 0;
  553.         else if ( newValue >  newMaxValue )
  554.             newValue = newMaxValue;
  555.         
  556.         SetControlValue( console->hScroll, newValue);
  557.     }
  558.  
  559.     //------------------------------------------------------------------------------
  560.     // Update lines
  561.     //
  562.     lines = (*teH)->nLines;
  563.     // since nLines isn’t right if the last character is a return, check for that case
  564.     if ( *( *(*teH)->hText + (*teH)->teLength - 1) == '\n' )
  565.         lines++;
  566.     
  567.     //------------------------------------------------------------------------------
  568.     // Adjust vertical scroll bars
  569.     //
  570.     oldValue = GetControlValue( console->vScroll);
  571.     oldMaxValue = GetControlMaximum( console->vScroll);
  572.     
  573.     newMaxValue = lines - ( ( (*teH)->viewRect.bottom - (*teH)->viewRect.top) / (*teH)->lineHeight);
  574.     if ( newMaxValue < 0 ) newMaxValue = 0;
  575.     SetControlMaximum( console->vScroll, newMaxValue);
  576.     
  577.     if ( operand == kCWInputText)
  578.     {
  579.         //
  580.         // Show the last line while inputing text <3>
  581.         //
  582.         newValue = newMaxValue;
  583.     }
  584.     else
  585.     {
  586.         newValue = ( (*teH)->viewRect.top - (*teH)->destRect.top) / (*teH)->lineHeight;
  587.         if ( newValue < 0 )
  588.             newValue = 0;
  589.         else if ( newValue >  newMaxValue )
  590.             newValue = newMaxValue;
  591.     }
  592.     
  593.     SetControlValue( console->vScroll, newValue);
  594.     
  595.     //------------------------------------------------------------------------------
  596.     // Show both controls
  597.     //
  598. //    ShowControl( console->vScroll);
  599. //    ShowControl( console->hScroll);
  600.  
  601.     //------------------------------------------------------------------------------
  602.     // Adjust TextEdit
  603.     //
  604.     deltaH = ( (*teH)->viewRect.left - (*teH)->destRect.left)
  605.              - GetControlValue( console->hScroll);
  606.     
  607.     deltaV = ( (*teH)->viewRect.top - (*teH)->destRect.top)
  608.            - ( GetControlValue( console->vScroll) * (*teH)->lineHeight);
  609.  
  610.     TEScroll( deltaH, deltaV, teH);
  611. }
  612.  
  613.  
  614. // ========================================================================================
  615. // UpdateTEAttribute
  616. // ========================================================================================
  617. static void
  618. UpdateTEAttribute( ConsoleWindowPtr console, Boolean redraw )
  619. {
  620.     GrafPtr        savePort;
  621.     WindowRef    window = GetWindowFromConsoleWindow( console);
  622.  
  623.     GetPort( &savePort);
  624.     SetPort( window);
  625.     
  626.     AdjustConsoleWindow( console, kCWChangeAttribute);
  627.  
  628.     if ( redraw)
  629.     {
  630.         Rect    viewRect;
  631.         
  632.         GetTERect( window, NULL, &viewRect);
  633.         EraseRect( &viewRect);
  634.         TEUpdate( &viewRect, console->textHandle);
  635.     }
  636.     
  637.     SetPort( savePort);
  638. }
  639.  
  640.  
  641. // ========================================================================================
  642. // ConsoleWindowCStrLen
  643. // ========================================================================================
  644. static SInt32
  645. ConsoleWindowCStrLen( const char *str )
  646. {
  647.     SInt32    len = -1;
  648.     
  649.     do
  650.     {
  651.         len++;
  652.     }
  653.     while ( *str++ );
  654.     
  655.     return( len );
  656. }
  657.  
  658.  
  659. // ========================================================================================
  660. // IsConsoleWindow
  661. // ========================================================================================
  662. static Boolean
  663. IsConsoleWindow( WindowRef window )
  664. {
  665.     if ( window != NULL)
  666.     {
  667.         return ( ((ConsoleWindowPtr)window)->signature == kCWSignature);
  668.     }
  669.     else
  670.     {
  671.         return false;
  672.     }
  673. }
  674.  
  675.  
  676. // ========================================================================================
  677. // DoConsoleWindowMouseDown
  678. // ========================================================================================
  679. static void
  680. DoConsoleWindowMouseDown( ConsoleWindowPtr console, EventRecord *theEvent,
  681.                           SInt16 partCode, Boolean *closed )
  682. {
  683.     WindowRef        window = GetWindowFromConsoleWindow( console);
  684.     
  685.     switch ( partCode )
  686.     {
  687.     case inContent:
  688.         if ( window != FrontWindow())
  689.             SelectWindow( window);
  690.         else
  691.             DoConsoleWindowContent( console, theEvent->where);
  692.         break;
  693.     
  694.     case inDrag:
  695.         DragWindow( window, theEvent->where, &qd.screenBits.bounds);
  696.         break;
  697.     
  698.     case inGrow:
  699.         DoConsoleWindowGrow( console, theEvent->where);
  700.         break;
  701.     
  702.     case inGoAway:
  703.         if ( TrackGoAway( window, theEvent->where))
  704.         {
  705.             *closed = true;    // <2> caller should dispose window
  706.         }
  707.         break;
  708.     
  709.     case inZoomIn:
  710.     case inZoomOut:
  711.         if ( TrackBox( window, theEvent->where, partCode))
  712.         {
  713.             GrafPtr        savePort;
  714.             
  715.             GetPort( &savePort);
  716.             SetPort( window);
  717.             
  718.             EraseRect( &window->portRect);
  719.             ZoomWindow( window, partCode, window == FrontWindow());
  720.             ResizeConsoleWindow( console);
  721.             
  722.             SetPort( savePort);
  723.         }
  724.         break;
  725.         
  726.     default:
  727.         break;
  728.     }
  729. }
  730.  
  731.  
  732. // ========================================================================================
  733. // DoConsoleWindowUpdate
  734. // ========================================================================================
  735. static void
  736. DoConsoleWindowUpdate( ConsoleWindowPtr console )
  737. {
  738.     GrafPtr        savePort;
  739.     WindowRef    window = GetWindowFromConsoleWindow( console);
  740.  
  741.     GetPort( &savePort);
  742.     SetPort( window);
  743.     
  744.     BeginUpdate( window);
  745.     
  746.     EraseRect( &window->portRect);
  747.     TEUpdate( &window->portRect, console->textHandle);
  748.     DrawGrowIcon( window);
  749.     DrawControls( window);
  750.     
  751.     EndUpdate( window);
  752.         
  753.     SetPort( savePort);
  754. }
  755.  
  756.  
  757. // ========================================================================================
  758. // DoConsoleWindowActivate
  759. // ========================================================================================
  760. static void
  761. DoConsoleWindowActivate( ConsoleWindowPtr console, Boolean toActive )
  762. {
  763.     WindowRef    window = GetWindowFromConsoleWindow( console);
  764.  
  765.     if ( toActive )
  766.     {
  767.         DrawGrowIcon( window);
  768.         HiliteControl( console->vScroll, 0);
  769.         HiliteControl( console->hScroll, 0);
  770.     }
  771.     else
  772.     {
  773.         DrawGrowIcon( window);
  774.         HiliteControl( console->vScroll, 255);
  775.         HiliteControl( console->hScroll, 255);
  776.     }
  777. }
  778.  
  779.  
  780. // ========================================================================================
  781. // DoConsoleWindowGrow
  782. // ========================================================================================
  783. static void
  784. DoConsoleWindowGrow( ConsoleWindowPtr console, Point startPt )
  785. {
  786.     WindowRef    window = GetWindowFromConsoleWindow( console);
  787.     Rect        growRect;
  788.     SInt32        growth;
  789.     
  790.     growRect.top = (*console->textHandle)->lineHeight * 10 + kCWTextMargin * 2;
  791.     growRect.left = 200;
  792.     growRect.bottom = kCMGrowMax;
  793.     growRect.right = kCMGrowMax;
  794.     
  795.     growth = GrowWindow( window, startPt, &growRect);
  796.  
  797.     if ( growth != 0L)
  798.     {
  799.         SizeWindow( window, growth & 0x0000FFFF, ( growth >> 16) & 0x0000FFFF, true);
  800.         ResizeConsoleWindow( console);
  801.     }
  802. }
  803.  
  804.  
  805. // ========================================================================================
  806. // ResizeConsoleWindow
  807. // ========================================================================================
  808. static void
  809. ResizeConsoleWindow( ConsoleWindowPtr console )
  810. {
  811.     GrafPtr        savePort;
  812.     WindowRef    window = GetWindowFromConsoleWindow( console);
  813.     
  814.     GetPort( &savePort);
  815.     SetPort( window);
  816.     
  817.     AdjustConsoleWindow( console, kCWResizeWindow);
  818.     InvalRect( &window->portRect);
  819.     
  820.     SetPort( savePort);
  821. }
  822.  
  823.  
  824. // ========================================================================================
  825. // DoConsoleWindowContent
  826. // ========================================================================================
  827. static void
  828. DoConsoleWindowContent( ConsoleWindowPtr console, Point startPt )
  829. {
  830.     GrafPtr                savePort;
  831.     WindowRef            window = GetWindowFromConsoleWindow( console);
  832.     Point                localPt;
  833.     SInt16                value, part, lineHeight;
  834.     ControlRef            targetControl;
  835.     ControlActionUPP    myTrackCntlUPP;
  836.  
  837.     GetPort( &savePort);
  838.     SetPort( window);
  839.     
  840.     localPt = startPt;
  841.     GlobalToLocal( &localPt);
  842.     
  843.     //    Set control RefCon to use internal of filter proc
  844.     SetControlReference( console->vScroll, (long)console);
  845.     SetControlReference( console->hScroll, (long)console);
  846.     
  847.     part = FindControl( localPt, window, &targetControl);
  848.     
  849.     if ( targetControl == console->vScroll)
  850.         myTrackCntlUPP = NewControlActionProc( MyVScrollFilter);
  851.     else
  852.         myTrackCntlUPP = NewControlActionProc( MyHScrollFilter);
  853.  
  854.     if ( part != 0)
  855.     {
  856.         switch ( part )
  857.         {
  858.         case kControlIndicatorPart:
  859.             value = GetControlValue( targetControl);
  860.             part = TrackControl( targetControl, localPt, NULL);
  861.             if ( part != 0)
  862.             {
  863.                 value -= GetControlValue( targetControl);
  864.                 if ( value != 0)
  865.                 {
  866.                     if ( targetControl == console->vScroll)
  867.                     {
  868.                         lineHeight = (*console->textHandle)->lineHeight;
  869.                         TEScroll( 0, value * lineHeight, console->textHandle);
  870.                     }
  871.                     else
  872.                     {
  873.                         TEScroll( value, 0, console->textHandle);
  874.                     }
  875.                 }
  876.             }
  877.             break;
  878.             
  879.         default:
  880.             value = TrackControl( targetControl, localPt, myTrackCntlUPP);
  881.             break;
  882.         }
  883.     }
  884.  
  885.     DisposeRoutineDescriptor( myTrackCntlUPP);
  886.     SetPort( savePort);
  887. }
  888.  
  889.  
  890. // ========================================================================================
  891. // MyVScrollFilter
  892. // ========================================================================================
  893. static pascal void
  894. MyVScrollFilter( ControlRef control, SInt16 part )
  895. {
  896.     SInt16                currentValue, maxValue, minValue;
  897.     SInt16                updateDelta, lineHeight;
  898.     ConsoleWindowPtr    console;
  899.     Rect                viewRect;
  900.     
  901.     updateDelta = currentValue = GetControlValue( control);
  902.     maxValue = GetControlMaximum( control);
  903.     minValue = GetControlMinimum( control);
  904.     
  905.     console = (ConsoleWindowPtr)GetControlReference( control);
  906.     
  907.     lineHeight = (*console->textHandle)->lineHeight;
  908.     GetTERect( (WindowRef)console, NULL, &viewRect);
  909.     
  910.     switch ( part )
  911.     {
  912.     case kControlPageUpPart:
  913.         currentValue -= ( viewRect.bottom - viewRect.top) / lineHeight;
  914.         if ( currentValue < minValue)
  915.             currentValue = minValue;
  916.         updateDelta -= currentValue;
  917.         break;
  918.         
  919.     case kControlPageDownPart:
  920.         currentValue += ( viewRect.bottom - viewRect.top) / lineHeight;
  921.         if ( currentValue > maxValue)
  922.             currentValue = maxValue;
  923.         updateDelta -= currentValue;
  924.         break;
  925.         
  926.     case kControlUpButtonPart:
  927.         currentValue -= 1;
  928.         if ( currentValue < minValue)
  929.             currentValue = minValue;
  930.         updateDelta -= currentValue;
  931.         break;
  932.         
  933.     case kControlDownButtonPart:
  934.         currentValue += 1;
  935.         if ( currentValue > maxValue)
  936.             currentValue = maxValue;
  937.         updateDelta -= currentValue;
  938.         break;
  939.         
  940.     default:
  941.         return;
  942.     }
  943.     
  944.     SetControlValue( control, currentValue);
  945.     if ( updateDelta != 0)
  946.     {
  947.         TEScroll( 0, updateDelta * lineHeight, console->textHandle);
  948.     }
  949. }
  950.  
  951.  
  952. // ========================================================================================
  953. // MyHScrollFilter
  954. // ========================================================================================
  955. static pascal void
  956. MyHScrollFilter( ControlRef control, SInt16 part )
  957. {
  958.     SInt16                currentValue, maxValue, minValue;
  959.     SInt16                updateDelta;
  960.     ConsoleWindowPtr    console;
  961.     Rect                viewRect;
  962.     
  963.     updateDelta = currentValue = GetControlValue( control);
  964.     maxValue = GetControlMaximum( control);
  965.     minValue = GetControlMinimum( control);
  966.     
  967.     console = (ConsoleWindowPtr)GetControlReference( control);
  968.     
  969.     GetTERect( (WindowRef)console, NULL, &viewRect);
  970.     
  971.     switch ( part )
  972.     {
  973.     case kControlPageUpPart:
  974.         currentValue -= viewRect.right - viewRect.left;
  975.         if ( currentValue < minValue)
  976.             currentValue = minValue;
  977.         updateDelta -= currentValue;
  978.         break;
  979.         
  980.     case kControlPageDownPart:
  981.         currentValue += viewRect.right - viewRect.left;
  982.         if ( currentValue > maxValue)
  983.             currentValue = maxValue;
  984.         updateDelta -= currentValue;
  985.         break;
  986.         
  987.     case kControlUpButtonPart:
  988.         currentValue -= kCMHorizScrollUnit;
  989.         if ( currentValue < minValue)
  990.             currentValue = minValue;
  991.         updateDelta -= currentValue;
  992.         break;
  993.         
  994.     case kControlDownButtonPart:
  995.         currentValue += kCMHorizScrollUnit;
  996.         if ( currentValue > maxValue)
  997.             currentValue = maxValue;
  998.         updateDelta -= currentValue;
  999.         break;
  1000.         
  1001.     default:
  1002.         return;
  1003.     }
  1004.     
  1005.     SetControlValue( control, currentValue);
  1006.     if ( updateDelta != 0)
  1007.     {
  1008.         TEScroll( updateDelta, 0, console->textHandle);
  1009.     }
  1010. }
  1011.  
  1012.  
  1013.